<?php
/**
 * create to 2021/6/24 15:39
 */

namespace app\channel\controller\sys_admin;


use app\AdminController;
use app\channel\model\ProxyLevelModel;
use app\channel\model\UpgradelogModel;
use think\facade\Session;

class Upgradelog extends AdminController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize(){
        parent::initialize();
        $this->Model = new UpgradelogModel();
    }
    /*------------------------------------------------------ */
    //-- 主页
    /*------------------------------------------------------ */
    public function index() {
        $this->getList(true);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {
        $search['report_type'] = input('report_type',0,'intval');
        $search['status'] = input('status',0,'intval');
        $search['proxy_id'] = input('proxy_id',0,'intval');
        $search['keyword'] = input('keyword','','trim');
        $where = [];

        $data = $this->getPageList($this->Model,$where);
        foreach ($data['list'] as &$v) {
            $now_proxy = ProxyLevelModel::where('proxy_id',$v['now_proxy_id'])->field('proxy_name')->find();
            $want_proxy = ProxyLevelModel::where('proxy_id',$v['want_proxy_id'])->field('proxy_name')->find();
            $v['now_proxy'] = $now_proxy['proxy_name'];
            $v['want_proxy'] = $want_proxy['proxy_name'];
            if ($v['createtime']){
                $v['create_time'] = date("Y-m-d H:i:s",$v['createtime']);
            }
        }
        $this->assign("data", $data);
        $this->assign("status", $this->Model->status);
        $this->assign("type", $this->Model->type);
        if ($runData == false){
            $data['content']= $this->fetch('sys_admin/upgradelog/list')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 详情
    /*------------------------------------------------------ */
    public function info() {
        $upgrade_log_id = input('id',0,'intval');
        if ($upgrade_log_id < 1){
            return $this->error('传参错误.');
        }
        $row = $this->Model->where('id',$upgrade_log_id)->find();
        if (empty($row)){
            return $this->error('没有找到相关记录.');
        }
        $row['create_time'] = date("Y-m-d H:i:s",$row['createtime']);
        $now_proxy = ProxyLevelModel::where('proxy_id',$row['now_proxy_id'])->field('proxy_name')->find();
        $want_proxy = ProxyLevelModel::where('proxy_id',$row['want_proxy_id'])->field('proxy_name')->find();
        $row['now_proxy'] = $now_proxy['proxy_name'];
        $row['want_proxy'] = $want_proxy['proxy_name'];
        $this->assign('row',$row->toArray());
        $this->assign('status',$this->Model->status);
        $this->assign("type", $this->Model->type);
        return $this->fetch('info');
    }

    /*------------------------------------------------------ */
    //-- 审核
    /*------------------------------------------------------ */
    public function check() {
        $id = input('id',0,'intval');
        $status = input('status',0,'intval');
        $refuse_content = input('reasons_pass');
        if ($id < 1){
            return $this->error('传参错误.');
        }
        $row = $this->Model->where('id',$id)->find();
        if (empty($row)){
            return $this->error('没有找到相关记录.');
        }
        $save_data = ['status'=>$status];
        if ($status==2) {
            $save_data['refuse_content'] = $refuse_content;
        }
        $res = $this->Model->where('id',$id)->update($save_data);
        if ($res < 1){
            return $this->error('更新审核结果失败.');
        }
        return $this->success('操作成功.');
    }
}